package control.utils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.sound.midi.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JOptionPane;
import javax.swing.plaf.SliderUI;

public class TocadorAudio {

	private AudioInputStream stream1;
	private AudioFormat format;
	private Clip clip;

	public void TocaAudio() {
		try {
			//if (stream1 == null) {
				stream1 = AudioSystem
						.getAudioInputStream(new File("chimes.wav"));

				format = stream1.getFormat();

				if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
					format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
							format.getSampleRate(), format
									.getSampleSizeInBits(), format
									.getChannels(), format.getFrameSize(),
							format.getFrameRate(), true); // big
					// endian
					stream1 = AudioSystem.getAudioInputStream(format, stream1);
				}
				// Create the clip
				DataLine.Info info = new DataLine.Info(Clip.class, stream1
						.getFormat(), ((int) stream1.getFrameLength() * format
						.getFrameSize()));
				clip = (Clip) AudioSystem.getLine(info);

				// This method does not return until the audio file is
				// completely
				// loaded

			//}
			
				System.out.println("Openning");
				clip.open(stream1);
			
			// Start playing
			clip.start();
			

			

		} catch (MalformedURLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (LineUnavailableException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (UnsupportedAudioFileException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
	}
}
